<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/29
 * Time: 9:43
 */
namespace app\index\controller;


use app\index\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\View;

class Account extends Controller{
    public function account(){
        return $this->fetch();
    }
    public function login(){
        return $this->fetch();
    }
    function checkLogin(){
        $email=input("post.email");
        $pwd=md5(input("post.pwd"));
        $arr=Db::table("user")->field('name')->where(["email"=>$email,"pwd"=>$pwd])->find();
        if ($arr){
            //登录成功
            session("login_user",$arr);
            $this->redirect("index/index/index");
        }
        else
            $this->error("用户名或密码错误");
    }
    public function reg(){
        return $this->fetch();
    }
    //注册成功添加到数据库中
    public function regSucess(){
        //接受传递过来的信息
        $name=input('post.name');
        $surn=input('post.surn');
        $email=input('post.email');
        $pwd=input('post.pwd');
        $data=["name"=>$name,"surn"=>$surn,"email"=>$email,"pwd"=>$pwd];
        $validate=Loader::validate("Checkform");
        if ($validate->check($data)==false)
            dump($validate->getError());
        else{
            $user=new \app\index\model\User();
            $user->data($data);
            $user->save();

            $this->redirect("index/account/login");
        }
    }
    //更新数据库  一条
    public function test(){
        //一条数据
        //$user=new User();
        //$user->save(["name"=>"test","pwd"=>"tesy"],["user_id"=>"1003"]);

        //多条数据
//        $user=new User();
//        $list=[["user_id"=>1003,"name"=>"aa","pwd"=>"aa"],["user_id"=>1002,"name"=>"bb"]];
//        $user->saveAll($list);
        //删除一条
//        echo User::destroy(1003);//根据主键删除
//        echo User::destroy(["name"=>"131"]);
//        //多条
//        //echo Db::table('user')->where('user_id','>',1003)->delete();
//        //查询
//        $user=User::get(1001)->find();
//        echo $user;
//        $obj=User::get(["name"=>"11"]);
//        echo $obj->pwd;

    }
}